package com.heima.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.heima.mapper.PartnerMapper;
import com.heima.pojo.PageBean;
import com.heima.pojo.Partner;
import com.heima.pojo.dto.PartnerQueryParamDto;
import com.heima.service.PartnerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class PatnreServiceImpl implements PartnerService {
    @Autowired
    private PartnerMapper partnerMapper;
    @Override
    public Boolean addPartner(Partner partner) {
        if( partnerMapper.selectPartnerByName(partner.getName()) != null && partnerMapper.selectPartnerByMobile(partner.getMobile())!=null )
        {
            return false;
        }
        partner.setCreateTime(LocalDateTime.now());
        partner.setUpdateTime(LocalDateTime.now());
        partner.setPassword(partner.getPassword());
        partnerMapper.addPartner(partner);
        return true;
    }

    @Override
    public PageBean selectPartner(PartnerQueryParamDto partnerDto) {
        //如果名字不是空 要进行判断，
        if (partnerDto.getName() != null || partnerDto.getName() != ""){
            if (partnerMapper.selectPartnerByName(partnerDto.getName()) != null){
                throw new RuntimeException("合作伙伴名重复 ");
            }
            PageHelper.startPage(partnerDto.getPageIndex(),partnerDto.getPageSize());
            List<Partner> list = partnerMapper.selectPartnerList(partnerDto);

            PageInfo<Partner> pageInfo = new PageInfo<>(list);

            return new PageBean(partnerDto.getPageIndex(),
                    partnerDto.getPageSize(),
                    (long) pageInfo.getPages(),
                    pageInfo.getTotal(),
                    list);
        }
        PageHelper.startPage(partnerDto.getPageIndex(),partnerDto.getPageSize());
        List<Partner> list = partnerMapper.selectPartnerList(partnerDto);

        PageInfo<Partner> pageInfo = new PageInfo<>(list);

        return new PageBean(partnerDto.getPageIndex(),
                partnerDto.getPageSize(),
                (long) pageInfo.getPages(),
                pageInfo.getTotal(),
                list);

    }

    @Override
    public Partner selectPartnerById(int id) {
        return partnerMapper.selectPartnerById(id);
    }

    @Override
    public Boolean updatePartner(int id, Partner partner) {
        //判断一下，不能和其他手机号一样，不能和其他名字一样
        if (partnerMapper.selectPartnerByName(partner.getName()) != null &&partnerMapper.selectPartnerByMobile(partner.getMobile()) != null){
            return false;
        }
        partner.setUpdateTime(LocalDateTime.now());
        partner.setId(id);
        partnerMapper.updatePartner(partner);

        return true;
    }

    @Override
    public Boolean deletePartner(int id) {
        if (partnerMapper.deletePartner(id) > 0){
            return true;
        }
        return false;
    }


}
